package com.powerteam.controller.crm;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.crm.Contacts;
import com.powerteam.service.crm.ContactsService;
import com.powerteam.vo.Result;
import com.powerteam.vo.crm.QueryContactsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 联系人页面相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/contacts")
public class ContactsController extends AuthorizedController {

    @Autowired
    private ContactsService contactsService;

    /**
     * 联系人页
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String contacts() {

        return "crm/contacts";
    }


    /**
     * 创建联系人信息
     *
     * @param contacts
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/9
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Contacts contacts) {

        contacts.setCreateBy(getUser().getUserId());
        return contactsService.insert(contacts);
    }

    /**
     * 分页查询联系人
     *
     * @param vo
     * @return PageInfo<Contacts>
     * @Author jinming.zhang
     * @Date 2022/1/9
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public PageInfo<Contacts> find(@RequestBody QueryContactsVO vo) {

        return contactsService.find(vo);
    }

    /**
     * 根据Id查找联系人信息
     *
     * @param contacts
     * @return Contacts
     * @Author jinming.zhang
     * @Date 2022/1/9
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Contacts findById(@RequestBody Contacts contacts) {

        return contactsService.findById(contacts.getContactsId());
    }

    /**
     * 修改联系人信息
     *
     * @param contacts
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/9
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Contacts contacts) {

        return contactsService.update(contacts);
    }

    /**
     * 删除联系人
     *
     * @param contactsId
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/4/13
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody Integer contactsId) {

        return contactsService.remove(contactsId);
    }

}
